package com.hankol.feature_wine_meal.respository

import android.os.Looper
import androidx.lifecycle.MutableLiveData
import com.baselibrary.base.BasicListBean
import com.baselibrary.base.mvvm.BaseApi
import com.baselibrary.bean.FriendListBean
import com.baselibrary.net.APIResponse
import com.baselibrary.net.MyHttpUtil
import com.baselibrary.utils.GsonUtils
import com.baselibrary.utils.SpUtils
import com.okhttp.comment.CommentBean
import com.hankol.feature_wine_meal.api.WineMealApi
import com.hankol.feature_wine_meal.bean.*
import com.okhttp.comment.CommentConstant
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.json.JSONObject

/**
 * @author: 关忆北、
 * @date: 2022/6/27
 * @Des：
 */
class WineMealRequestManager {
    companion object {
        @Volatile
        private var instance: WineMealRequestManager? = null

        fun getInstance(): WineMealRequestManager {
            return instance ?: synchronized(this) {
                instance ?: WineMealRequestManager().also { instance = it }
            }
        }
    }

    /**
     *特色菜系刷新
     */
    fun reqCuisine(liveData: MutableLiveData<ArrayList<CuisineRefreshBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(WineMealApi::class.java).reqCuisine(it)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : APIResponse<ArrayList<CuisineRefreshBean>>(this) {
                        override fun onSuccess(t: ArrayList<CuisineRefreshBean>, json: String) {
                            liveData.value = t
                        }

                        override fun onError(e: Throwable) {
                            super.onError(e)
                            liveData.value = null
                        }
                    })
        }
    }

    /**
     *特色菜系换一铺
     */
    fun reqChangeBatch(categoryId: Long, liveData: MutableLiveData<ArrayList<AppetizerDiet>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(WineMealApi::class.java).reqChangeBatch(it, categoryId)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : APIResponse<ArrayList<AppetizerDiet>>(this) {
                        override fun onSuccess(t: ArrayList<AppetizerDiet>, json: String) {
                            liveData.value = t
                        }

                        override fun onError(e: Throwable) {
                            super.onError(e)
                        }
                    })
        }
    }

    /**
     *下酒菜详情
     */
    fun reqFoodInfo(id: Long, foodInfoLD: MutableLiveData<FoodDetailsBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(WineMealApi::class.java).reqFoodInfo(it, id)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : APIResponse<FoodDetailsBean>(this) {
                        override fun onSuccess(t: FoodDetailsBean, json: String) {
                            foodInfoLD.value = t
                        }

                        override fun onError(e: Throwable) {
                            super.onError(e)
                        }
                    })
        }
    }

    /**
     *热门菜谱
     */
    fun reqHotFood(size: Int,page: Int, hotFoodLiveData: MutableLiveData<BasicListBean<FoodDetailsBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(WineMealApi::class.java).reqHotFood(it, size, page)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : APIResponse<BasicListBean<FoodDetailsBean>>(this) {
                        override fun onSuccess(t: BasicListBean<FoodDetailsBean>, json: String) {
                            hotFoodLiveData.value = t
                        }

                        override fun onError(e: Throwable) {
                            super.onError(e)
                        }
                    })
        }
    }

    /**
     *精选列表推荐
     */
    fun reqFoodRecommend(hotFoodLiveData: MutableLiveData<ArrayList<RecommendFoodBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                    .getApi(WineMealApi::class.java)
                    .reqRecommend(it)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : APIResponse<ArrayList<RecommendFoodBean>>(this) {
                        override fun onSuccess(t: ArrayList<RecommendFoodBean>, json: String) {
                            hotFoodLiveData.value = t
                        }

                        override fun onError(e: Throwable) {
                            super.onError(e)
                        }
                    })
        }
    }

    /**
     *精选推荐详情页
     */
    fun reqRecommendInfo(id: Long, page: Int, hotFoodLiveData: MutableLiveData<BasicListBean<FoodDetailsBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                    .getApi(WineMealApi::class.java)
                    .reqRecommendInfo(it, id, page)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : APIResponse<BasicListBean<FoodDetailsBean>>(this) {
                        override fun onSuccess(t: BasicListBean<FoodDetailsBean>, json: String) {
                            hotFoodLiveData.value = t
                        }

                        override fun onError(e: Throwable) {
                            super.onError(e)
                        }
                    })
        }
    }


    /**
     * @param topCommentId 置顶评论的id。。默认传0就行
     */
    fun reqCommentList(commentListLiveData: MutableLiveData<CommentBean.DataBean>, type: Int, page: Int, typeId: Long, pid: Long, topCommentId: Long) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(WineMealApi::class.java)
                    .reqCommentList(it, CommentConstant.COMMENT_LOAD_COUNT, type, typeId, page, pid, topCommentId)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : APIResponse<CommentBean.DataBean>(this) {
                        override fun onSuccess(t: CommentBean.DataBean, json: String) {
                            commentListLiveData.value = t
                        }

                        override fun onError(e: Throwable) {
                            super.onError(e)
                            commentListLiveData.value = null
                        }
                    })
        }
    }


    /**
     * 特色菜系
     */
    fun reqCharacteristic(commentListLiveData: MutableLiveData<ArrayList<CharacteristicBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(WineMealApi::class.java)
                    .reqCharacteristic(it)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : APIResponse<ArrayList<CharacteristicBean>>(this) {
                        override fun onSuccess(t: ArrayList<CharacteristicBean>, json: String) {
                            commentListLiveData.value = t
                        }

                        override fun onError(e: Throwable) {
                            super.onError(e)
                            commentListLiveData.value = null
                        }
                    })
        }
    }

    /**
     * 特色菜系
     */
    fun reqGoConfig(commentListLiveData: MutableLiveData<String>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(WineMealApi::class.java)
                    .reqGoConfig(it)
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : APIResponse<String>(this) {
                        override fun onSuccess(t: String, json: String) {
                            commentListLiveData.value = t
                        }
                    })
        }
    }


}